Skip to main content

Random Permutation

np.random.permutation(arr)

Randomly shuffles/reorders arr.

Input:
arr : array, list, or integer
If array or list, randomly reorder array or list. If integer (e.g. 5), randomly reorder np.arange(5) (aka np.array([0, 1, 2, 3, 4])).
Returns:
Shuffled/reordered array
Return Type:
array

example_array = np.array([1, 2, 3, 4, 5])
example_array

array([1, 2, 3, 4, 5])

for i in range(5):
print(f'{i + 1}st random permutation:')
print(np.random.permutation(example_array), end='\n\n')

1st random permutation: [3 5 4 2 1]

2st random permutation: [5 4 1 2 3]

3st random permutation: [3 5 4 1 2]

4st random permutation: [5 1 2 4 3]

5st random permutation: [2 4 1 3 5]